$(() => {
    const token = GetQueryString('token');
    let area_id = GetQueryString('area_id');
    let cat_id = GetQueryString('id');
    let cat_name = GetQueryString('cat_name');
    let title = GetQueryString('cat_name');
    let page = 1;
    let pageSize = 5;
    let area_code = '';
    let keywords = '';
    let ret = 1;
    if (cat_id) {
        companyLists(page, cat_id);
    } else {
        companyLists(page);
    }
    $('.cat_name').attr('data-cat_name', cat_name);
    $('.cat_id').attr('data-cat_id', cat_id);

    if (area_id == 2) {
        $('.tabLine').hide();
        $('.childBtn').show();
        // 水暖专区当页面选中一级分类中，存在二级分类，点击显示二级分类
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Plat|watar_company_category' },
            dataType: 'json',
            success: function(res) {
                if (res.status == 1) {
                    let data = res.data;
                    data.forEach(items => {
                        if (items.id == cat_id) {
                            $('.childBtn').on('click', function() {
                                let html = '';

                            items.sub_list.forEach(item => {
                                console.log(items.sub_list)
                                // item.forEach(item => {
                                    html += `
                                    <a href="javascript:;" data-cat_id="${item.id}" class="shList cityItem">${item.bname}</a>
                                    `;
                                })
                                    $('.letter26').html(html);
                                    $('.cityItem').on('click', function() {
                                        let cat_id = $(this).attr('data-cat_id');
                                        let childCategory = $(this).html();
                                        $('.cat_name').attr('data-cat_childname', childCategory);
                                        let cat_childName = $('.cat_name').attr('data-cat_childname');
                                        $('.cat_id').attr('data-cat_id', cat_id);
                                        $('.comLists').html('');
                                        if (area_code) {
                                            companyLists(page, cat_id, area_code, '');
                                        } else {
                                            companyLists(page, cat_id, '', '');
                                        }
                                        $('.childCategory').html(cat_childName);
                                        dialogClose();
                                        $(".tabItem").attr("data-num", 1).removeClass("on");
                                    })
                                })
                            // })
                        }
                    })
                }
            }
        })
    }

    function companyLists(page, cat_id, area_code, keywords) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': "Company|more_company", 'area_id': area_id, 'p': page, 'page_size': pageSize, 'cat_id': cat_id, 'area_code': area_code, 'keywords': keywords },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    let tab = '';
                    cat_name = $('.cat_name').attr('data-cat_name');
                    $('.hetitle').html(title);

                    // 当前综合选项
                    if (cat_id) {
                        $('.category').html(cat_name);
                    }

                    data.list.forEach(item => {
                        html += `
                        <div class="comList">`
                        if (item.company_level >= 1) {
                            html += `<a href="shop_details_company.html?company_id=${item.id}&token=${token}" class="comHead">`
                        } else {
                            html += `<a href="shop_details.html?company_id=${item.id}&token=${token}" class="comHead">`
                        }
                        html += `<div class="comHeadLeft">
                                    <img class="headImg" src="${item.company_logo == '' ? '../img/user.jpg' : item.company_logo}" alt="${item.company_name}">
                                    <div class="headlineTopChild">
                                        <p>${item.company_name}</p>`
                        if (item.level_logo != '') {
                            html += `<img src="${item.level_logo}" alt="${item.level_name}" class="userLevel"/>`
                        }
                        html += `</div>    
                                </div>
                                <p class="comHeadRight">进店</p>
                            </a>
                            <div class="comImgs">`
                        item.goods_list.forEach(g => {
                            html += `<a href="comp_shopDetail.html?id=${g.id}&token=${token}"><img class="comImg" src="${g.goods_thumb == '' ? '../img/user.jpg' : g.goods_thumb}" alt="${g.goods_name}"></a>`
                        })

                        html += `</div>
                            <p class="comDesc">
                                主营：${item.main_business}
                            </p>
                            <p class="comAddr">
                                地址：${item.area_name}
                                <span>浏览量${item.read_count}</span>
                            </p>
                        </div>
                    `;
                    })
                    if (data.list.length < pageSize) {
                        $('.notMore').css('display', 'flex');
                    }
                    $('.comLists').append(html);
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    }

    // 搜索
    $('.searchIcon').on('click', function() {
        let keys = $('.inputBtn>input').val();
        let cat_id = $('.cat_id').attr('data-cat_id');
        let area_code = $('.city_area').attr('data-area_code');
        $('.comLists').html('');
        $('.notMore').css('display', 'none');
        page = 1;
        if (cat_id || area_code) {
            companyLists(page, cat_id, area_code, keys);
        } else {
            companyLists(page, '', '', keys);
        }
        $('.inputBtn>input').val('');
        $('.searchInput').hide();
    })

    if (area_id == 1) {
        // 石材综合匹配
        $('.typeBtn').on('click', function() {
            $.ajax({
                type: "post",
                url: url,
                data: { 'method': "Plat|block_company_category" },
                dataType: 'json',
                success: function(res) {
                    // console.log(res);
                    let data = res.data;
                    let html = '';
                    if (res.status == 1) {
                        data.forEach(item => {
                            html += `
                            <a href="javascript:;" data-id="${item.id}" data-cat_id="${item.id}" data-cat_type="${item.cat_type}" class="shList cityItem">${item.cat_name}</a>
                         `;
                        })
                        $('.classifyLists').html(html);
                    }
                    $('.cityItem').on('click', function() {
                        let catId = $(this).attr('data-cat_id');
                        let cat_type = $(this).attr('data-cat_type');
                        let id = $(this).attr('data-id');
                        let category = $(this).html();
                        $(".cat_name").attr("data-cat_name", category);
                        $('.cat_id').attr('data-cat_id', catId);
                        let cat_id = $('.cat_id').attr('data-cat_id');
                        if (cat_id && cat_type == 1) {
                            let area_code = $('.city_area').attr('data-area_code');
                            $('.comLists').html('');
                            $('.notMore').css('display', 'none')
                            page = 1;
                            if (area_code) {
                                companyLists(page, cat_id, area_code, '');
                            } else {
                                companyLists(page, cat_id, '', '');
                            }
                            let cat_name = $('.cat_name').attr('data-cat_name');
                            $('.category').html(cat_name);
                        } else if (cat_type == 2) {
                            location.href = "more_goods_n.html?area_id=1&cat_name=" + category + '&category_id=' + id + '&token=' + token;
                        }
                        $(".tabItem").attr("data-num", 1).removeClass("on");
                        dialogClose();
                    })
                }
            })
        })
    } else {
        // 水暖一、二级匹配
        $('.typeBtn').on('click', function() {
            $.ajax({
                type: "post",
                url: url,
                data: { 'method': "Plat|watar_company_category" },
                dataType: 'json',
                success: function(res) {
                    // console.log(res);
                    let data = res.data;
                    let html = '';
                    if (res.status == 1) {
                        data.forEach(item => {
                            let arr = [];
                            item.sub_list.forEach(e => {
                                arr.push([e.id, e.bname]);
                            })
                            html += `
                                <a href="javascript:;" data-area_id="${item.area_id}" data-list="${arr}" data-cat_id="${item.id}" class="shList cityItem">${item.bname}</a>
                            `;
                            $('.classifyLists').html(html);
                            let area_code = $('.city_area').attr('data-area_code');

                            //如果没有二级就用一级cat_id
                            $('.cityItem').on('click', function() {
                                let arr_list = $(this).attr('data-list');
                                arr_list = arr_list.split(',');
                                let cat_id = $(this).attr('data-cat_id');
                                $('.cat_id').attr('data-cat_id', cat_id);
                                if (arr_list.length == 1) {
                                    $('.childCategory').html('分类');
                                    let category = $(this).html();
                                    $('.cat_name').attr('data-cat_name', category);
                                    let cat_name = $('.cat_name').attr('data-cat_name');
                                    $('.comLists').html('');
                                    $('.notMore').css('display', 'none');
                                    $('.category').html(cat_name);
                                    page = 1;
                                    if (area_code) {
                                        companyLists(page, cat_id, area_code, '');
                                    } else {
                                        companyLists(page, cat_id, '', '');
                                    }
                                    dialogClose();
                                    $(".tabItem").attr("data-num", 1).removeClass("on");
                                    $('.childBtn').on('click', function() {
                                        $('.letter26').html('');
                                    })
                                } else {
                                    let html = '';
                                    $('.childCategory').html('分类');
                                    // $('.letter26').html('');
                                    let arr1 = $(this).attr("data-list");
                                    let category = $(this).html();
                                    $('.cat_name').attr('data-cat_name', category);
                                    let cat_name = $('.cat_name').attr('data-cat_name');
                                    arr1 = arr1.split(",");
                                    for (let i = 0; i < arr1.length; i += 2) {
                                        html += `
                                            <a href="javascript:;" data-cat_id="${arr1[i]}" class="shList cityItem">${arr1[i+1]}</a>
                                         `;
                                    }
                                    $('.comLists').html('');
                                    $('.notMore').css('display', 'none');
                                    $('.category').html(cat_name);
                                    page = 1;
                                    if (area_code) {
                                        companyLists(page, cat_id, area_code, '');
                                    } else {
                                        companyLists(page, cat_id, '', '');
                                    }
                                    dialogClose();
                                    $(".tabItem").attr("data-num", 1).removeClass("on");
                                    $('.childBtn').on('click', function() {
                                        $('.letter26').html(html);
                                        $('.cityItem').on('click', function() {
                                            let cat_id = $(this).attr('data-cat_id');
                                            let childCategory = $(this).html();
                                            $('.cat_name').attr('data-cat_childname', childCategory);
                                            let cat_childName = $('.cat_name').attr('data-cat_childname');
                                            $('.cat_id').attr('data-cat_id', cat_id);
                                            $('.comLists').html('');
                                            $('.notMore').css('display', 'none');
                                            page = 1;
                                            if (area_code) {
                                                companyLists(page, cat_id, area_code, '');
                                            } else {
                                                companyLists(page, cat_id, '', '');
                                            }
                                            $('.childCategory').html(cat_childName);
                                            dialogClose();
                                            $(".tabItem").attr("data-num", 1).removeClass("on");
                                        })
                                    })
                                }
                            })
                        })
                    } else {
                        alert(res.message);
                    }
                }
            })
        })
    }

    // 地区匹配
    $(document).on('click', '.cityBtn', function() {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': "Api|get_area_list", 'pcode': 1 },
            dataType: 'json',
            success: function(res) {
                // // console.log(res);
                let data = res.data;
                let html = '';
                let all = '';
                if (res.status == 1) {
                    all = `<a href="javascript:;" data-code="" class="shList cityItem">全部</a>`;
                    data.forEach(item => {
                        html += `
                            <a href="javascript:;" data-code="${item.area_code}" class="shList cityItem">${item.area_name}</a>
                         `;
                    })
                    $('.areaLists').html(all + html);
                }
                $('.cityItem').on('click', function() {
                    let area_code = $(this).attr('data-code');
                    $('.city_area').attr('data-area_code', area_code);
                    let area = $(this).html();
                    let cat_id = $('.cat_id').attr('data-cat_id');
                    if (area_code == '') {
                        $('.area').html('地区');
                        $('.comLists').html('');
                        $('.notMore').css('display', 'none');
                        page = 1;
                        if (cat_id) {
                            companyLists(page, cat_id, '', '');
                        } else {
                            companyLists(page, '', '', '');
                        }
                    }
                    if (area_code) {
                        $('.comLists').html('');
                        $('.notMore').css('display', 'none');
                        page = 1;
                        if (cat_id) {
                            companyLists(page, cat_id, area_code, '');
                        } else {
                            companyLists(page, '', area_code, '');
                        }
                        $('.area').html(area);
                    }
                    dialogClose();
                    $(".tabItem").attr("data-num", 1).removeClass("on");
                })
            }
        })
    })

    // 下拉加载更多
    $(window).scroll(function() {
        if ($(document).scrollTop() >= $(document).height() - $(window).height() - 1) {
            // console.log('我到底了');
            if ($('.notMore').css('display') != "flex") {
                let keys = $('.inputBtn>input').val();
                let cat_id = $('.cat_id').attr('data-cat_id');
                let area_code = $('.city_area').attr('data-area_code');
                page++;
                if (cat_id || area_code || keys) {
                    companyLists(page, cat_id, area_code, keys);
                } else {
                    companyLists(page);
                }
            }
        }
    })
})